import sys
import os
import time

pwd = os.path.dirname(os.path.realpath(__file__))
print(pwd)
sys.path.append(pwd + "./")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ihouse.settings")

import django

django.setup()

from house.models import District
import pandas as pd

df = pd.read_csv("./统计局_行政区划及代码表_2020.csv")
df = df.loc[:, df["provincetr"] == "北京市"]

count = df.count()["village_num"]
# assert 0
start_time = time.time()
tmp_time = time.time()
print(start_time)

# ##########################
# provincetr      北京市
# city            市辖区
# city_num        110100000000
# county          东城区
# county_num      110101000000
# town            东华门街道
# town_num        110101001000
# village         多福巷社区居委会
# village_code    111
# village_num     110101001001
data_count = 0

province_df = df.drop_duplicates(["provincetr"], keep="first").loc[:, ["provincetr"]]
province_list = [District(name=province.provincetr, district_level=0) for _, province in province_df.iterrows()]
District.objects.bulk_create(province_list)

for _, province in province_df.iterrows():
    # print(_, province)
    province_obj = District.objects.get(name=province.provincetr)

    city_df = df.drop_duplicates(["city_num"], keep="first").loc[
        df["provincetr"] == province.provincetr, ["city", "city_num"]]
    city_list = [District(parent=province_obj, district_level=1,
                          name=city.city, district_id=city.city_num
                          ) for _, city in city_df.iterrows()]
    District.objects.bulk_create(city_list)

    for _, city in city_df.iterrows():
        # print(_, city.city, city.city_num)
        city_obj = District.objects.get(district_id=city.city_num)

        county_df = df.drop_duplicates(["county_num"], keep="first").loc[
            df["city_num"] == city.city_num, ["county", "county_num"]]
        county_list = [District(parent=city_obj, district_level=2,
                                name=county.county, district_id=county.county_num
                                ) for _, county in county_df.iterrows()]
        District.objects.bulk_create(county_list)

        for _, county in county_df.iterrows():
            # print(_, county.county, county.county_num)
            print(province.provincetr, city.city, county.county)
            county_obj = District.objects.get(district_id=county.county_num)

            town_df = df.drop_duplicates(["town_num"], keep="first").loc[
                df["county_num"] == county.county_num, ["town", "town_num"]]
            town_list = [District(parent=county_obj, district_level=3,
                                  name=town.town, district_id=town.town_num
                                  ) for _, town in town_df.iterrows()]
            District.objects.bulk_create(town_list)

            for _, town in town_df.iterrows():
                # print(_, town.town, town.town_num)
                town_obj = District.objects.get(district_id=town.town_num)

                village_df = df.loc[df["town_num"] == town.town_num, ["village", "village_code", "village_num"]]
                village_list = [District(parent=town_obj, district_level=4,
                                         name=village.village, district_id=village.village_num,
                                         village_code=village.village_code
                                         ) for _, village in village_df.iterrows()]
                District.objects.bulk_create(village_list)
                data_count += len(village_list)
                print(time.time() - start_time, "导入数据：{} - {}/{}".format(len(village_list), data_count, count))
                # print(time.time() - tmp_time, "导入数据：{} - {}/{}".format(len(village_list), data_count, count))
                tmp_time = time.time()
